package com.summer.peony.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.system.pojo.SysDict;
import com.summer.peony.system.service.SysDictService;

@RequestMapping("/system/dict")
@RestController
public class SysDictController {

	@Autowired
	SysDictService sysDictService;

	/**
	 * @Description: TODO(新增字典)
	 * @param sysDict
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月25日 下午1:30:06
	 * @throws:
	 */
	@PostMapping("/add")
	public AjaxResult add(@RequestBody SysDict sysDict) {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			Integer add = sysDictService.add(sysDict);
			if (add == 1) {
				ajaxResult.put("code", 0);
				ajaxResult.put("msg", "添加成功");
				return ajaxResult;
			}
			ajaxResult.put("code", 900009);
			ajaxResult.put("msg", "字典类型已存在");
			return ajaxResult;
		} catch (Exception e) {
			e.printStackTrace();
			ajaxResult.put("code", 999999);
			ajaxResult.put("msg", "添加失败");
			return ajaxResult;
		}
	}

	/**
	 * @Description: TODO(编辑字典)
	 * @param sysDict
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月25日 下午1:30:20
	 * @throws:
	 */
	@PutMapping("/edit")
	public AjaxResult edit(@RequestBody SysDict sysDict) {
		Integer upd = sysDictService.edit(sysDict);
		AjaxResult ajaxResult = new AjaxResult();
		if (upd == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 900010);
		ajaxResult.put("msg", "字典类型已存在");
		return ajaxResult;
	}

	/**
	 * @Description: TODO(删除字典)
	 * @param id
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月25日 下午1:34:08
	 * @throws:
	 */
	@DeleteMapping("/remov/{id}")
	public AjaxResult remov(@PathVariable("id") Integer id) {
		Integer del = sysDictService.remov(id);
		if (del == 1) {
			return new AjaxResult(Type.SUCCESS, "删除成功");
		}
		return new AjaxResult(Type.ERROR, "删除失败");
	}

	/**
	 * @Description: TODO(分页查询列表)
	 * @param name
	 * @param code
	 * @param pageNum
	 * @param pageSize
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月25日 下午1:36:16
	 * @throws:
	 */
	@GetMapping("/list")
	public AjaxResult list(String label, String value, String type, Integer pageNum, Integer pageSize) {
		PageInfo<SysDict> list = sysDictService.list(label, value, type, pageNum, pageSize);
		if (list != null) {
			return new AjaxResult(Type.SUCCESS, "查询部门列表", list);
		}
		return new AjaxResult(Type.SUCCESS, "数据为空", list);
	}
}
